{% extends 'base.html' %}

{% block title %}种植数据清洗{% endblock %}

{% block content %}
<div class="card mb-4">
    <div class="card-header bg-primary text-white">
        <h4 class="mb-0">异常数据检测</h4>
    </div>
    <div class="card-body">
        <form method="get" class="mb-4">
            <div class="row g-3">
                <div class="col-md-3">
                    <input type="text" class="form-control" name="search_crop" value="{{ search_crop }}" placeholder="搜索作物类型...">
                </div>
                <div class="col-md-3">
                    <input type="text" class="form-control" name="search_region" value="{{ search_region }}" placeholder="搜索种植区域...">
                </div>
                <div class="col-md-2">
                    <input type="date" class="form-control" name="date_from" value="{{ date_from }}" placeholder="开始日期">
                </div>
                <div class="col-md-2">
                    <input type="date" class="form-control" name="date_to" value="{{ date_to }}" placeholder="结束日期">
                </div>
                <div class="col-md-1">
                    <button type="submit" class="btn btn-primary w-100">搜索</button>
                </div>
                <div class="col-md-1">
                    <a href="{{ url_for('cleaning.planting_clean') }}" class="btn btn-secondary w-100">重置</a>
                </div>
            </div>
        </form>

        <div class="table-responsive">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>作物类型</th>
                        <th>面积(亩)</th>
                        <th>种植区域</th>
                        <th>种植日期</th>
                        <th>异常原因</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    {% for item in anomalies %}
                    <tr>
                        <td>{{ item.crop_type }}</td>
                        <td>{{ item.area }}</td>
                        <td>{{ item.region }}</td>
                        <td>{{ item.planting_date.strftime('%Y-%m-%d') }}</td>
                        <td>
                            {% if item.area <= 0 %}面积异常{% endif %}
                            {% if item.area > 10000 %}面积过大{% endif %}
                            {% if item.planting_date > now %}日期异常{% endif %}
                            {% if not item.crop_type %}作物类型为空{% endif %}
                            {% if not item.region %}区域为空{% endif %}
                        </td>
                        <td>
                            <button type="button" class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#fixModal{{ item.id }}">
                                修正
                            </button>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>

        {% if anomalies_pagination.pages > 1 %}
        <nav aria-label="Page navigation" class="mt-4">
            <ul class="pagination justify-content-center">
                <li class="page-item {% if not anomalies_pagination.has_prev %}disabled{% endif %}">
                    <a class="page-link" href="{% if anomalies_pagination.has_prev %}{{ url_for('cleaning.planting_clean', page=anomalies_pagination.prev_num, search_crop=search_crop, search_region=search_region, date_from=date_from, date_to=date_to) }}{% else %}#{% endif %}">
                        上一页
                    </a>
                </li>
                
                {% for page in anomalies_pagination.iter_pages(left_edge=2, left_current=2, right_current=3, right_edge=2) %}
                    {% if page %}
                        <li class="page-item {% if page == anomalies_pagination.page %}active{% endif %}">
                            <a class="page-link" href="{{ url_for('cleaning.planting_clean', page=page, search_crop=search_crop, search_region=search_region, date_from=date_from, date_to=date_to) }}">
                                {{ page }}
                            </a>
                        </li>
                    {% else %}
                        <li class="page-item disabled">
                            <span class="page-link">...</span>
                        </li>
                    {% endif %}
                {% endfor %}
                
                <li class="page-item {% if not anomalies_pagination.has_next %}disabled{% endif %}">
                    <a class="page-link" href="{% if anomalies_pagination.has_next %}{{ url_for('cleaning.planting_clean', page=anomalies_pagination.next_num, search_crop=search_crop, search_region=search_region, date_from=date_from, date_to=date_to) }}{% else %}#{% endif %}">
                        下一页
                    </a>
                </li>
            </ul>
        </nav>
        {% endif %}
    </div>
</div>

<div class="card">
    <div class="card-header bg-primary text-white">
        <h4 class="mb-0">重复数据检测</h4>
    </div>
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>作物类型</th>
                        <th>种植区域</th>
                        <th>种植日期</th>
                        <th>重复次数</th>
                    </tr>
                </thead>
                <tbody>
                    {% for item in duplicates %}
                    <tr>
                        <td>{{ item.crop_type }}</td>
                        <td>{{ item.region }}</td>
                        <td>{{ item.planting_date.strftime('%Y-%m-%d') }}</td>
                        <td>{{ item.count }}</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
</div>

{% for item in anomalies %}
<div class="modal fade" id="fixModal{{ item.id }}" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">修正异常数据</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form action="{{ url_for('cleaning.fix_planting_anomaly', id=item.id) }}" method="post">
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label">作物类型</label>
                        <input type="text" class="form-control" name="crop_type" value="{{ item.crop_type }}">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">面积(亩)</label>
                        <input type="number" step="0.01" class="form-control" name="area" value="{{ item.area }}">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">种植区域</label>
                        <input type="text" class="form-control" name="region" value="{{ item.region }}">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">种植日期</label>
                        <input type="date" class="form-control" name="planting_date" value="{{ item.planting_date.strftime('%Y-%m-%d') }}">
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-primary">保存</button>
                </div>
            </form>
        </div>
    </div>
</div>
{% endfor %}
{% endblock %} 